home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / string.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  11.7 KB  |  385 lines  |  [TEXT/ALFA]

  1. # Commands covered:  string
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # SCCS: @(#) string.test 1.15 97/07/02 16:49:27
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. test string-1.1 {string compare} {
  18.     string compare abcde abdef
  19. } -1
  20. test string-1.2 {string compare} {
  21.     string c abcde ABCDE
  22. } 1
  23. test string-1.3 {string compare} {
  24.     string compare abcde abcde
  25. } 0
  26. test string-1.4 {string compare} {
  27.     list [catch {string compare a} msg] $msg
  28. } {1 {wrong # args: should be "string compare string1 string2"}}
  29. test string-1.5 {string compare} {
  30.     list [catch {string compare a b c} msg] $msg
  31. } {1 {wrong # args: should be "string compare string1 string2"}}
  32.  
  33. test string-2.1 {string first} {
  34.     string first bq abcdefgbcefgbqrs
  35. } 12
  36. test string-2.2 {string first} {
  37.     string fir bcd abcdefgbcefgbqrs
  38. } 1
  39. test string-2.3 {string first} {
  40.     string f b abcdefgbcefgbqrs
  41. } 1
  42. test string-2.4 {string first} {
  43.     string first xxx x123xx345xxx789xxx012
  44. } 9
  45. test string-2.5 {string first} {
  46.     string first "" x123xx345xxx789xxx012
  47. } -1
  48. test string-2.6 {string first} {
  49.     list [catch {string first a} msg] $msg
  50. } {1 {wrong # args: should be "string first string1 string2"}}
  51. test string-2.7 {string first} {
  52.     list [catch {string first a b c} msg] $msg
  53. } {1 {wrong # args: should be "string first string1 string2"}}
  54.  
  55. test string-3.1 {string index} {
  56.     string index abcde 0
  57. } a
  58. test string-3.2 {string index} {
  59.     string i abcde 4
  60. } e
  61. test string-3.3 {string index} {
  62.     string index abcde 5
  63. } {}
  64. test string-3.4 {string index} {
  65.     list [catch {string index abcde -10} msg] $msg
  66. } {0 {}}
  67. test string-3.5 {string index} {
  68.     list [catch {string index} msg] $msg
  69. } {1 {wrong # args: should be "string index string charIndex"}}
  70. test string-3.6 {string index} {
  71.     list [catch {string index a b c} msg] $msg
  72. } {1 {wrong # args: should be "string index string charIndex"}}
  73. test string-3.7 {string index} {
  74.     list [catch {string index a xyz} msg] $msg
  75. } {1 {expected integer but got "xyz"}}
  76.  
  77. test string-4.1 {string last} {
  78.     string la xxx xxxx123xx345x678
  79. } 1
  80. test string-4.2 {string last} {
  81.     string last xx xxxx123xx345x678
  82. } 7
  83. test string-4.3 {string last} {
  84.     string las x xxxx123xx345x678
  85. } 12
  86. test string-4.4 {string last} {
  87.     list [catch {string last a} msg] $msg
  88. } {1 {wrong # args: should be "string last string1 string2"}}
  89. test string-4.5 {string last} {
  90.     list [catch {string last a b c} msg] $msg
  91. } {1 {wrong # args: should be "string last string1 string2"}}
  92.  
  93. test string-5.1 {string length} {
  94.     string length "a little string"
  95. } 15
  96. test string-5.2 {string length} {
  97.     string le ""
  98. } 0
  99. test string-5.3 {string length} {
  100.     list [catch {string length} msg] $msg
  101. } {1 {wrong # args: should be "string length string"}}
  102. test string-5.4 {string length} {
  103.     list [catch {string length a b} msg] $msg
  104. } {1 {wrong # args: should be "string length string"}}
  105.  
  106. test string-6.1 {string match} {
  107.     string match abc abc
  108. } 1
  109. test string-6.2 {string match} {
  110.     string m abc abd
  111. } 0
  112. test string-6.3 {string match} {
  113.     string match ab*c abc
  114. } 1
  115. test string-6.4 {string match} {
  116.     string match ab**c abc
  117. } 1
  118. test string-6.5 {string match} {
  119.     string match ab* abcdef
  120. } 1
  121. test string-6.6 {string match} {
  122.     string match *c abc
  123. } 1
  124. test string-6.7 {string match} {
  125.     string match *3*6*9 0123456789
  126. } 1
  127. test string-6.8 {string match} {
  128.     string match *3*6*9 01234567890
  129. } 0
  130. test string-6.9 {string match} {
  131.     string match a?c abc
  132. } 1
  133. test string-6.10 {string match} {
  134.     string match a??c abc
  135. } 0
  136. test string-6.11 {string match} {
  137.     string match ?1??4???8? 0123456789
  138. } 1
  139. test string-6.12 {string match} {
  140.     string match {[abc]bc} abc
  141. } 1
  142. test string-6.13 {string match} {
  143.     string match {a[abc]c} abc
  144. } 1
  145. test string-6.14 {string match} {
  146.     string match {a[xyz]c} abc
  147. } 0
  148. test string-6.15 {string match} {
  149.     string match {12[2-7]45} 12345
  150. } 1
  151. test string-6.16 {string match} {
  152.     string match {12[ab2-4cd]45} 12345
  153. } 1
  154. test string-6.17 {string match} {
  155.     string match {12[ab2-4cd]45} 12b45
  156. } 1
  157. test string-6.18 {string match} {
  158.     string match {12[ab2-4cd]45} 12d45
  159. } 1
  160. test string-6.19 {string match} {
  161.     string match {12[ab2-4cd]45} 12145
  162. } 0
  163. test string-6.20 {string match} {
  164.     string match {12[ab2-4cd]45} 12545
  165. } 0
  166. test string-6.21 {string match} {
  167.     string match {a\*b} a*b
  168. } 1
  169. test string-6.22 {string match} {
  170.     string match {a\*b} ab
  171. } 0
  172. test string-6.23 {string match} {
  173.     string match {a\*\?\[\]\\\x} "a*?\[\]\\x"
  174. } 1
  175. test string-6.24 {string match} {
  176.     string match ** ""
  177. } 1
  178. test string-6.25 {string match} {
  179.     string match *. ""
  180. } 0
  181. test string-6.26 {string match} {
  182.     string match "" ""
  183. } 1
  184. test string-6.27 {string match} {
  185.     string match \[a a
  186. } 1
  187. test string-6.28 {string match} {
  188.     list [catch {string match a} msg] $msg
  189. } {1 {wrong # args: should be "string match pattern string"}}
  190. test string-6.29 {string match} {
  191.     list [catch {string match a b c} msg] $msg
  192. } {1 {wrong # args: should be "string match pattern string"}}
  193.  
  194. test string-7.1 {string range} {
  195.     string range abcdefghijklmnop 2 14
  196. } {cdefghijklmno}
  197. test string-7.2 {string range} {
  198.     string range abcdefghijklmnop 7 1000
  199. } {hijklmnop}
  200. test string-7.3 {string range} {
  201.     string range abcdefghijklmnop 10 e
  202. } {klmnop}
  203. test string-7.4 {string range} {
  204.     string range abcdefghijklmnop 10 9
  205. } {}
  206. test string-7.5 {string range} {
  207.     string range abcdefghijklmnop -3 2
  208. } {abc}
  209. test string-7.6 {string range} {
  210.     string range abcdefghijklmnop -3 -2
  211. } {}
  212. test string-7.7 {string range} {
  213.     string range abcdefghijklmnop 1000 1010
  214. } {}
  215. test string-7.8 {string range} {
  216.     string range abcdefghijklmnop -100 end
  217. } {abcdefghijklmnop}
  218. test string-7.9 {string range} {
  219.     list [catch {string range} msg] $msg
  220. } {1 {wrong # args: should be "string range string first last"}}
  221. test string-7.10 {string range} {
  222.     list [catch {string range a 1} msg] $msg
  223. } {1 {wrong # args: should be "string range string first last"}}
  224. test string-7.11 {string range} {
  225.     list [catch {string range a 1 2 3} msg] $msg
  226. } {1 {wrong # args: should be "string range string first last"}}
  227. test string-7.12 {string range} {
  228.     list [catch {string range abc abc 1} msg] $msg
  229. } {1 {bad index "abc": must be integer or "end"}}
  230. test string-7.13 {string range} {
  231.     list [catch {string range abc 1 eof} msg] $msg
  232. } {1 {bad index "eof": must be integer or "end"}}
  233. test string-7.14 {string range} {
  234.     string range abcdefghijklmnop end end
  235. } {p}
  236. test string-7.15 {string range} {
  237.     string range abcdefghijklmnop e 1000
  238. } {p}
  239.  
  240. test string-8.1 {string trim} {
  241.     string trim "    XYZ      "
  242. } {XYZ}
  243. test string-8.2 {string trim} {
  244.     string trim "\t\nXYZ\t\n\r\n"
  245. } {XYZ}
  246. test string-8.3 {string trim} {
  247.     string trim "  A XYZ A    "
  248. } {A XYZ A}
  249. test string-8.4 {string trim} {
  250.     string trim "XXYYZZABC XXYYZZ" ZYX
  251. } {ABC }
  252. test string-8.5 {string trim} {
  253.     string trim "    \t\r      "
  254. } {}
  255. test string-8.6 {string trim} {
  256.     string trim {abcdefg} {}
  257. } {abcdefg}
  258. test string-8.7 {string trim} {
  259.     string trim {}
  260. } {}
  261. test string-8.8 {string trim} {
  262.     string trim ABC DEF
  263. } {ABC}
  264. test string-8.9 {string trim} {
  265.     list [catch {string trim} msg] $msg
  266. } {1 {wrong # args: should be "string trim string ?chars?"}}
  267. test string-8.10 {string trim} {
  268.     list [catch {string trim a b c} msg] $msg
  269. } {1 {wrong # args: should be "string trim string ?chars?"}}
  270.  
  271. test string-9.1 {string trimleft} {
  272.     string trimleft "    XYZ      "
  273. } {XYZ      }
  274. test string-9.2 {string trimleft} {
  275.     list [catch {string trimleft} msg] $msg
  276. } {1 {wrong # args: should be "string trimleft string ?chars?"}}
  277.  
  278. test string-10.1 {string trimright} {
  279.     string trimright "    XYZ      "
  280. } {    XYZ}
  281. test string-10.2 {string trimright} {
  282.     string trimright "   "
  283. } {}
  284. test string-10.3 {string trimright} {
  285.     string trimright ""
  286. } {}
  287. test string-10.4 {string trimright errors} {
  288.     list [catch {string trimright} msg] $msg
  289. } {1 {wrong # args: should be "string trimright string ?chars?"}}
  290. test string-10.5 {string trimright errors} {
  291.     list [catch {string trimg a} msg] $msg
  292. } {1 {bad option "trimg": must be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, trimright, wordend, or wordstart}}
  293.  
  294. test string-11.1 {string tolower} {
  295.     string tolower ABCDeF
  296. } {abcdef}
  297. test string-11.2 {string tolower} {
  298.     string tolower "ABC  XyZ"
  299. } {abc  xyz}
  300. test string-11.3 {string tolower} {
  301.     string tolower {123#$&*()}
  302. } {123#$&*()}
  303. test string-11.4 {string tolower} {
  304.     list [catch {string tolower} msg] $msg
  305. } {1 {wrong # args: should be "string tolower string"}}
  306. test string-11.5 {string tolower} {
  307.     list [catch {string tolower a b} msg] $msg
  308. } {1 {wrong # args: should be "string tolower string"}}
  309.  
  310. test string-12.1 {string toupper} {
  311.     string toupper abCDEf
  312. } {ABCDEF}
  313. test string-12.2 {string toupper} {
  314.     string toupper "abc xYz"
  315. } {ABC XYZ}
  316. test string-12.3 {string toupper} {
  317.     string toupper {123#$&*()}
  318. } {123#$&*()}
  319. test string-12.4 {string toupper} {
  320.     list [catch {string toupper} msg] $msg
  321. } {1 {wrong # args: should be "string toupper string"}}
  322. test string-12.5 {string toupper} {
  323.     list [catch {string toupper a b} msg] $msg
  324. } {1 {wrong # args: should be "string toupper string"}}
  325.  
  326. test string-13.1 {string wordend} {
  327.     list [catch {string wordend a} msg] $msg
  328. } {1 {wrong # args: should be "string wordend string index"}}
  329. test string-13.2 {string wordend} {
  330.     list [catch {string wordend a b c} msg] $msg
  331. } {1 {wrong # args: should be "string wordend string index"}}
  332. test string-13.3 {string wordend} {
  333.     list [catch {string wordend a gorp} msg] $msg
  334. } {1 {expected integer but got "gorp"}}
  335. test string-13.4 {string wordend} {
  336.     string wordend abc. -1
  337. } 3
  338. test string-13.5 {string wordend} {
  339.     string wordend abc. 100
  340. } 4
  341. test string-13.6 {string wordend} {
  342.     string wordend "word_one two three" 2
  343. } 8
  344. test string-13.7 {string wordend} {
  345.     string wordend "one .&# three" 5
  346. } 6
  347. test string-13.8 {string wordend} {
  348.     string worde "x.y" 0
  349. } 1
  350.  
  351. test string-14.1 {string wordstart} {
  352.     list [catch {string word a} msg] $msg
  353. } {1 {ambiguous option "word": must be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, trimright, wordend, or wordstart}}
  354. test string-14.2 {string wordstart} {
  355.     list [catch {string wordstart a} msg] $msg
  356. } {1 {wrong # args: should be "string wordstart string index"}}
  357. test string-14.3 {string wordstart} {
  358.     list [catch {string wordstart a b c} msg] $msg
  359. } {1 {wrong # args: should be "string wordstart string index"}}
  360. test string-14.4 {string wordstart} {
  361.     list [catch {string wordstart a gorp} msg] $msg
  362. } {1 {expected integer but got "gorp"}}
  363. test string-14.5 {string wordstart} {
  364.     string wordstart "one two three_words" 400
  365. } 8
  366. test string-14.6 {string wordstart} {
  367.     string wordstart "one two three_words" 2
  368. } 0
  369. test string-14.7 {string wordend} {
  370.     string wordstart "one two three_words" -2
  371. } 0
  372. test string-14.8 {string wordend} {
  373.     string wordstart "one .*&^ three" 6
  374. } 6
  375. test string-14.9 {string wordend} {
  376.     string wordstart "one two three" 4
  377. } 4
  378.  
  379. test string-15.1 {error conditions} {
  380.     list [catch {string gorp a b} msg] $msg
  381. } {1 {bad option "gorp": must be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, trimright, wordend, or wordstart}}
  382. test string-15.2 {error conditions} {
  383.     list [catch {string} msg] $msg
  384. } {1 {wrong # args: should be "string option arg ?arg ...?"}}
  385.